package online.sh.cn.fy.shops;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;

/**
 * 校园店地址
 */
@WebServlet("/shoplist")
public class ShopListServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		JSONObject json=new JSONObject();
		TokenUtils to=new TokenUtils();
		if(null!=openid && ""!=openid){
			String uuid=CookieUtil.getCookieValueByName(request, "uuid");
			if(uuid.equals("0")){
				try {
					openid=DES3Util.decrypt(openid);
				} catch (Exception e) {
					log(e.getMessage());
				}
				Map map=to.selectFyUserinfoMapAll(openid);
				String school=(String)map.get("school");
				Map schoolshop=to.checkSchoolShopBySchool(school);
				String result1="";
				if(null==schoolshop || schoolshop.isEmpty()){
					result1="";
				}else{
					result1="<li class='clearfix current'><a href='javascript:void(0);'>"
					    +"<div class='fl img-box'><img src='img/shlogo/"+schoolshop.get("shimage").toString()+"' alt=''></div>"
					    +"<div class='fr txt-box'><h5>"+schoolshop.get("shopname").toString()+"</h5>"
						+"<h6>——"+schoolshop.get("schoolname").toString()+"</h6>"
						+"<div class='shopList-address'>"+schoolshop.get("shopaddress").toString()+"</div>"
						+"<div class='shopList-phone'>"+schoolshop.get("shoptel").toString()+"</div></div></a></li>";
				}
				json.put("currentShop", result1);
				List shoplist=to.checkSchoolShop();
				String result2="";
				if (null!=shoplist &&shoplist.size()!=0) {
					int size=shoplist.size();	
					for (int i = 0; i < size; i++) {
						Map map2=(Map) shoplist.get(i);
						result2+="<li class='clearfix'><a href='javascript:void(0);'>"
								+"<div class='fl img-box'><img src='img/shlogo/"+map2.get("shimage").toString()+"' alt=''></div>"
								+"<div class='fr txt-box'><h5>"+map2.get("shopname").toString()+"</h5>"
								+"<h6 id='shname' value='"+map2.get("schoolem").toString()+"'>——"+map2.get("schoolname").toString()+"</h6>"
								+"<div class='shopList-phone'>"+map2.get("shoptel").toString()+"</div></div></a></li>";
					}
					json.put("result", "0");
					json.put("shoplist", result2);
					ResponseUtils.renderJson(response, json.toString());
				}else{
					json.put("result", "1");
					ResponseUtils.renderJson(response, json.toString());
				}
			}else{
				String result="";
				json.put("currentShop", result);
				List shoplist=to.checkSchoolShop();
				if (null!=shoplist) {
					int size=shoplist.size();	
					for (int i = 0; i < size; i++) {
						Map map2=(Map) shoplist.get(i);
						result+="<li class='clearfix'><a href='javascript:void(0);'>"
								+"<div class='fl img-box'><img src='img/shlogo/"+map2.get("shimage").toString()+"' alt=''></div>"
								+"<div class='fr txt-box'><h5>"+map2.get("shopname").toString()+"</h5>"
								+"<h6 id='shname' value='"+map2.get("schoolem").toString()+"'>——"+map2.get("schoolname").toString()+"</h6>"
								+"<div class='shopList-phone'>"+map2.get("shoptel").toString()+"</div></div></a></li>";
					}
					json.put("result", "0");
					json.put("shoplist", result);
					ResponseUtils.renderJson(response, json.toString());
				}else{
					json.put("result", "1");
					ResponseUtils.renderJson(response, json.toString());
				}
			}
		}else{
			json.put("result", "1");
			ResponseUtils.renderJson(response, json.toString());
		}
	}

}
